﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using SkiaSharp;
using VOL.Core.Extensions;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.WorkforceManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.NurseManage;
using Yitter.IdGenerator;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace VOL.SmartMecial.Services.NurseManage
{
    public class PatientServices : IPatientServices
    {
        private readonly IBaseRepository<Entity.DomainModels.Bed> _bedRep;
        private readonly IBaseRepository<Entity.DomainModels.Patient> _patientRepository;
        private readonly IBaseRepository<NurseGroupModel> _nurseGroupRepository;//人员分组

        public PatientServices(IBaseRepository<Entity.DomainModels.Bed> bedRep, IBaseRepository<Patient> patientRepository, IBaseRepository<NurseGroupModel> nurseGroupRepository)
        {

            _bedRep = bedRep;
            _patientRepository = patientRepository;
            _nurseGroupRepository = nurseGroupRepository;
   
        }

        public async Task<List<PatientDTO>> ShowPatient()
        {
            var leftShow = from a in _bedRep.GetAll()
                           join b in _patientRepository.GetAll() on a.BedCode equals b.BedCode into c
                           from b in c.DefaultIfEmpty()
                           select new PatientDTO
                           {
                               PatientCode = b.PatientCode,
                               Name = b.Name,
                               OfficeCode = b.OfficeCode,
                               WardAreaCode = b.WardAreaCode,
                               NursingUnit = b.NursingUnit,
                               WardCode = b.WardCode,
                               BedCode = a.BedCode,
                               Sex = b.Sex,
                               Age = b.Age,
                               IDNo = b.IDNo,
                               Phone = b.Phone,
                               Address = b.Address,
                               SOSName = b.SOSName,
                               Diagnosis = b.Diagnosis
                           };
            return await Task.FromResult(leftShow.ToList());
        }

        public async Task<WebResponseContent> GetByPatientId(string patientCode)
        {
            var patient = await _patientRepository.GetAll().Where(m => m.PatientCode == patientCode).ToListAsync();
            var res = new WebResponseContent();
            res.OK("查询成功", patient);
            return await Task.FromResult(res);
        }

        /// <summary>
        /// 患者床位列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">条数</param>
        /// <returns>返回</returns>
        public Task<WebResponseContent> GetPatient(int pageIndex, int pageSize)
        {
            var patientList = _patientRepository.GetAll();
            int count = patientList.Count();
            var patients = patientList.OrderBy(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var pageGrid = new PageGridData<Patient>();

            pageGrid.total = count;
            pageGrid.rows = patients;
            var res = new WebResponseContent();
            res.OK("显示成功", pageGrid);
            return Task.FromResult(res);
        }

        /// <summary>
        /// 人员分组
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> ShowNurseGroup()
        {
            var nurseGrouplist =await  _nurseGroupRepository.GetAll().ToListAsync();
            var res = new WebResponseContent();
            res.OK("查询成功", nurseGrouplist);
            return await Task.FromResult(res);
        }

        /// <summary>
        /// 回显患者信息
        /// </summary>
        /// <param name="patientCode">患者编号</param>
        /// <returns>返回</returns>
        public async Task<WebResponseContent> GetPatientDetail(string patientCode)
        {
            var patientList = await _patientRepository.GetAll().FirstOrDefaultAsync(o => o.PatientCode == patientCode);
            var res = new WebResponseContent();
            res.OK("显示成功", patientList);
            return await Task.FromResult(res);

        }

        /// <summary>
        /// 人员分组的添加
        /// </summary>
        /// <returns></returns>
        public async Task<int> AddNurseGroup(NurseGroupModel nurseGroup)
        {
            var i = await _nurseGroupRepository.AddAsync(nurseGroup);
            return i;
        }

        /// <summary>
        /// 人员分组的编辑
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpdNurseGroup(NurseGroupModel nurseGroup)
        {
            var i = await _nurseGroupRepository.UpdateAsync(nurseGroup);
            return i;
        }

        /// <summary>
        /// 人员分组的移除
        /// </summary>
        /// <returns></returns>
        public async Task<int> DelNurseGroup(int id)
        {
            var entity = _nurseGroupRepository.GetAll().FirstOrDefault(x => x.Id == id);
            if (entity == null)
            {
                return 0;
            }
           var i=await _nurseGroupRepository.Remove(entity);
            return i;

        }
    }
}
